local initst = false
function Kekaishi:OnGameRulesStateChange()
	local nNewState = GameRules:State_Get()
	if nNewState == DOTA_GAMERULES_STATE_STRATEGY_TIME then
		--print( "OnGameRulesStateChange: Strategy Time" )
		if not initst then
			self:ForceAssignHeroes()
			self:initGameData()

			_G.block = require("block")
			_G.gameControl = require("gameControl")
			_G.roleData = require "roleData"
			
			initst = true
		end
	elseif nNewState == DOTA_GAMERULES_STATE_HERO_SELECTION then
		--print( "OnGameRulesStateChange: Hero Selection" )

	elseif nNewState == DOTA_GAMERULES_STATE_PRE_GAME then
		--print( "OnGameRulesStateChange: Pre Game" )

	elseif nNewState == DOTA_GAMERULES_STATE_GAME_IN_PROGRESS then
		--print( "OnGameRulesStateChange: Game In Progress" )
		if not _G.bIsInited then
			
			
			
			createTimerBar(10,function(  )
				EmitGlobalSound("ui.npe_objective_complete")
				self:doCreateInfo()
				gameControl.startRound(1)
				local pos = GameRules.blocks[1].unit:GetAbsOrigin()
				for i=0,MAX_PLAYER do
					local hero = PlayerResource:GetSelectedHeroEntity(i)
					if hero then
						local gpos = GetGroundPosition(pos,hero)
						hero:SetAbsOrigin(gpos)
					end
				end
			end,"#TIP_BEFORE_START")
			
			_G.bIsInited = true
			_G.spawn_timer = Entities:CreateByClassname("info_target") -- Entities:FindByClassname(nil, 'CWorld')
			_G.spawn_timer:SetThink(gameControl.onGameThink, self, "custom_timers", 0.01)
		end
	end
end

function Kekaishi:ExecuteOrderFilter(keys)
	local ordertype = keys.order_type
	local id = keys.issuer_player_id_const
	local hero = PlayerResource:GetSelectedHeroEntity(id)
	-- if hero:HasModifier("modifier_medusa_stone_gaze_stone") or hero:HasModifier("MODIFIER_STATE_COMMAND_RESTRICTED") then
	-- 	return false
	-- end

	if ordertype ~= DOTA_UNIT_ORDER_CAST_NO_TARGET and ordertype ~= DOTA_UNIT_ORDER_CAST_TARGET and ordertype ~= DOTA_UNIT_ORDER_CAST_POSITION then
		return false
	end
	--[[
		每走一步之后的回调函数可以在这里触发
	]]

	return true
end

function Kekaishi:OnDamageFilter( keys )
	return false
end

--9位数就有可能溢出，最好是有可能超过9位数的数值要转化成字符串发过去
function Kekaishi:doCreateInfo(hPlayer)
	local infos = {}
	local playerlist = GameRules.playerlist
	for i,v in ipairs(playerlist) do
		local t = {
			playerid = v,
			steamid = tostring(PlayerResource:GetSteamID(v)),
			gold = _getGold(v),
		}
		table.insert(infos,t)
	end
	if hPlayer then
		CustomGameEventManager:Send_ServerToPlayer(hPlayer,"S2C_CREATE_PLAYER_INFO",{infos = infos})
	else
		CustomGameEventManager:Send_ServerToAllClients("S2C_CREATE_PLAYER_INFO", {infos = infos})
	end
end

function Kekaishi:OnPlayerReconnect( keys )
	local player = PlayerResource:GetPlayer(keys.PlayerID)
	if player == nil then return end
	--创建玩家信息栏
	self:doCreateInfo(player)

	--特效附加【停车税+地皮拥有者】
	block.reCreateParticle()
end

function Kekaishi:C2S_PUSH_GOODS( nothing,data )
	local owner = data.owner
	local gold = data.gold
	local itemIndex = data.itemIndex
	if gold then
		roleData.pushTrading(gold,owner,true)
	end
	if itemIndex then
		roleData.pushTrading(itemIndex,owner,false)
	end
end

function Kekaishi:C2S_PAIMAI_GIVEUP( nothing,data )
	local curid = roleData.getCurPMId()
	local playerId = data.PlayerID
	if curid == playerId then
		roleData.giveupPM(playerId)
	end
end

function Kekaishi:C2S_PAIMAI(nothing,data)
	local playerId = data.PlayerID
	local curid = roleData.getCurPMId()
	print(curid == playerId)
	if curid == playerId then
		roleData.surePM(playerId,tonumber(data.gold))
	end
end

function Kekaishi:C2S_CREATE_CITYNAME( nothing,data )
	local blockInfo = GameRules.blocks
	local citynames = {}
	for i,city in ipairs(blockInfo) do
		if city.suit then
			local t = {name=city.name,owner=city.owner,gold=city.gold}
			citynames[block.getUnitIndexByIdx(i)] = t
		end
	end
	local id = data.PlayerID
	local player = PlayerResource:GetPlayer(id)
	CustomGameEventManager:Send_ServerToPlayer(player, "C2S_CREATE_CITYNAME",citynames)
end

function Kekaishi:C2S_GET_CITY_DATA( nothing,data )
	local cityid = block.getCityByPosition(data.pos)
	
	if cityid then
		local cityData = roleData.getCityById(cityid)
		if cityData.suit then
			local id = data.PlayerID
			local player = PlayerResource:GetPlayer(id)
			local t = {
				name = cityData.name,
				gold = cityData.gold,
				lv = cityData.lv,
				build = cityData.build,
				owner = cityData.owner,
				lvdesc = LVDESC[cityData.suit],
				chezhan = cityData.chezhan,
			}
			CustomGameEventManager:Send_ServerToPlayer(player, "C2S_GET_CITY_DATA",t)
		end
	end
end

function Kekaishi:C2S_CANCLE_TRADE( nothing,data )
	local id = data.PlayerID
	print("M.C2S_CANCLE_TRADE")
	roleData.cancleTrade(id,true)
end

function Kekaishi:C2S_COMPLETE_TRADE( nothing,data )
	local id = data.PlayerID
	roleData.sureTrade(id)
end

function Kekaishi:S2C_CHOICE_DONE( nothing,data )
	local opt = data.opt
	local id = data.PlayerID
	local cityid = data.cityid
	roleData.doChoice(id,cityid,opt)
	
end

function Kekaishi:C2S_GET_UNITNAME( nothing,data )
	local blockdata = GameRules.blocks or {}
	local send = {}
	for i,v in ipairs(blockdata) do
		if v.suit then
			send[v.unit:entindex()] = v.name
		end
	end
	local id = data.PlayerID
	local player = PlayerResource:GetPlayer(id)
	CustomGameEventManager:Send_ServerToPlayer(player, "S2C_GET_UNITNAME",send)
end

function Kekaishi:C2S_GET_TURNTABLE( nothing,data )
	local zpstrs = block.getZpstrs()
	local id = data.PlayerID
	local player = PlayerResource:GetPlayer(id)
	CustomGameEventManager:Send_ServerToPlayer(player, "S2C_GET_TURNTABLE",{elements = zpstrs,length = #zpstrs})
end

function Kekaishi:OnDisconnect(keys)

	local name = keys.name
	local networkid = keys.networkid
	local reason = keys.reason
	local userid = keys.userid

	if GameRules.custom_state == STATE_PAIMAI then
		roleData.giveupPM(userid)
	elseif GameRules.custom_state == STATE_TRADING then
		roleData.cancleTrade(userid,true)
	end
end

function Kekaishi:C2S_GIVEUPGAME( nothing,data )
	local id = data.PlayerID
	gameControl.giveupGame(id)
end